var configId = getQueryString('id') ? getQueryString('id') : ''
$(function () {

  var options = {
    url: api + "/scabpm/bpm/userAgencyLog/listJson",
    listType: 'bpm',
    updateUrl: '/pages/bpm/instance/instanceImageDialog',
    method: 'post',
    modalName: "流程代理记录",
    queryParams: function(params) {
      return $.common.serializeNotNull('limit=' + params.limit + '&offset=' + params.offset + '&bual.config_id_^VE=' + configId +'&order=asc'+ '&' + $('form').serialize());
    },
    columns: [
      {
        field: 'flowInstanceName',
        title: '流程名称',
      },
      {
        field: 'taskName',
        title: '代理节点',
      },
      {
        field: 'approverName',
        title: '代理人',
      },
      {
        field: 'approveTime',
        title: '审批时间',
      },
      {
        field: 'approveStatus',
        title: '处理结果',
        formatter: function (value, row, index) {
          return statusDict(value)
        }
      },
      {
        field: 'createTime',
        title: '创建时间',
      },
      {
        title: '管理',
        formatter: function (value, row, index) {
          var actions = [];
          var name = '流程图【'+row.defName+'】'
          var instanceId,defId,taskId
          instanceId = row.flowInstanceId ? row.flowInstanceId : ''
          defId = row.defId ? row.defId : ''
          taskId = row.taskId ? row.taskId : ''
          actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="flowImage(\'' + instanceId + '\',\'' + defId + '\',\'' + taskId + '\')"><i class="fa fa-eye"></i>流程图</a> ');
          return actions.join('');
        }
      }]
  };
  $.table.init(options);
});

function flowImage(instanceId,defId,taskId){
  var url = '/bpm/instance/instanceImageDialog?instanceId=' + (instanceId?instanceId:'') + '&defId=' + (defId?defId:'') + '&taskId=' + (taskId?taskId:'')
  $.modal.openFullBpm('流程图', url)
}

function statusDict(item){
  var dict = {
    'awaiting_check': {text:'待处理',color:'label-warning'},
    'agree': {text:'同意',color:'label-info'},
    'oppose': {text:'反对',color:'label-danger'},
    'reject': {text:'驳回',color:'label-danger'},
    'signOppose': {text:'会签反对',color:'label-danger'},
    'signPass': {text:'会签通过',color:'label-success'},
    'turn': {text:'转办',color:'label-navy'}
  }
  return '<span class="label '+(dict[item] ? dict[item].color : '')+'">'+(dict[item] ? dict[item].text : item)+'</span>'
}

function remove(id) {
  table.set();
  $.modal.confirm("确定删除该条" + table.options.modalName + "信息吗？", function () {
    var url = $.common.isEmpty(id) ? table.options.removeUrl : table.options.removeUrl.replace("{id}", id);
    $.operate.post(url,{},function(result){
      if (result.isOk){
        $.modal.msgSuccess(result.msg == null ? '操作成功' : result.msg);
        $.table.refresh();
      }else{
        $.modal.alertError(result.msg);
      }
      $.modal.closeLoading();
    });
  });
}